using UnityEngine;

public class LogHandler : UILogBehaviour 
{
    public int fontSize = 20;
    public int maxLine = 24;
    public int maxLineChar = 60;
	public Vector2 scrollPosition = Vector2.zero;

	string _log;
	string _logText;
	string stackTrace;
	LogType logType;
	string exceptionFilePath;

	void Awake ()
	{
        Init();
	}

    public void Init()
    {
        if(Application.isEditor)
        {
            return;
        }

        if (exceptionFilePath != null)
        {
            return;
        }

        exceptionFilePath = Application.persistentDataPath + "/" + "exception.txt";
        CommonUtil.Delete(exceptionFilePath);

        CommonUtil.Delete(Application.persistentDataPath + "/" + "error.txt");

        Application.logMessageReceived += HandleLog;
    }

    void OnDestroy()
    {
        Application.logMessageReceived -= HandleLog;
    }

	public void showException()
	{
		string str = CommonUtil.fileToString(exceptionFilePath);
		if(str != null)
		{
			_log = str;
		}
	}

	void HandleLog(string log, string stackTrace, LogType type)
	{
        if (type == LogType.Exception || type == LogType.Error)
        {
            System.IO.File.AppendAllText(Application.persistentDataPath + "/" + "error.txt", log + "\n" + stackTrace + "\n\n\n");
        }

		if (type == LogType.Exception)
		{
			if(log != null && this._log == null)
			{
                if(!Application.isEditor && !SwitchConfig.isEnableGuiLog)
                {
                    if (!System.IO.File.Exists(exceptionFilePath))
                    {
                        CommonUtil.stringToFile(log, exceptionFilePath);
                    }
                }

				if(stackTrace != null)
				{
					log += '\n' + stackTrace;
				}

                int max = maxLineChar;
				string s = "";
				int index = 0;
				int lineNum = 0;
				int i = 0;
                // for(;i < log.Length; i++)
				// {
				// 	if(i - index >= max || log[i] == '\n')
				// 	{
				// 		s += log.Substring(index, i - index + 1);
				// 		if(i - index >= max && i + 1 < log.Length && log[i + 1] != '\n')
				// 		{
				// 			s += "\n";
				// 		}
				// 		index = i + 1;

				// 		lineNum++;
				// 	}
				// }

				// if(index < log.Length)
				// {
				// 	s += log.Substring(index, i - index);
				// }
				// log = s;

				// this._log = log;
				// this._logText = log;
				// if(lineNum >= maxLine){
				// 	this._logText = log.Substring(0, maxLine*maxLineChar - 10);
				// }
				for(;i < log.Length && lineNum < maxLine; i++)
				{
					if(i - index >= max || log[i] == '\n')
					{
						s += log.Substring(index, i - index + 1);
						if(i - index >= max && i + 1 < log.Length && log[i + 1] != '\n')
						{
							s += "\n";
						}
						index = i + 1;

						lineNum++;
					}
				}

				if(index < log.Length)
				{
					s += log.Substring(index, i - index);
				}
				log = s;

				this._log = log;
			}
		}
	}

	protected override void myOnGUI()
	{
		GUI.color = Color.yellow;
		if(_log != null)
		{	
//			Rect position = GuiUtil.getLabelSize(_logText, fontSize, LabelPos.HC | LabelPos.VC);
//			GUI.BeginScrollView(position, scrollPosition, position);
            GuiUtil.Label(_log, fontSize, LabelPos.HC | LabelPos.VC);
//			GUI.EndScrollView();
		}
	}
}